package com.aboat.action;

import com.aboat.Application;
import com.aboat.licenseVerify.FileTool;
import com.aboat.licenseVerify.Permitted;
import com.aboat.licenseVerify.PermittedMessage;
import com.aboat.licenseVerify.cpInfo;
import com.aboat.view.GBC;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * 注册
 *
 * @author aboat
 * @email clear_windy@yeah.net
 * @create 2017-04-09 10:23
 */
public class RegisterListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {

        JDialog jd = new JDialog(Application.sFrame,"注册",true);
        jd.setSize(400,150);
        jd.setLocationRelativeTo(null);
        GridBagLayout gridbag = new GridBagLayout();
        jd.setLayout(gridbag);

        JLabel titleLabel = new JLabel("将电脑特征值发送给卖家，获取许可文件。再将收到的许可文件载入即可。");
        jd.add(titleLabel,new GBC(0,0,3,1).setWeight(50,50).setInsets(1));

        JLabel eigenLabel = new JLabel("电脑特征值:");
        JTextField eigenvalue = new JTextField (50);
        eigenvalue.setEditable(false);
        try {
            eigenvalue.setText(cpInfo.getFeature());
        }catch (Exception ex){

        }
        jd.add(eigenLabel,new GBC(0,1,1,1).setWeight(50,50).setInsets(1));
        jd.add(eigenvalue,new GBC(1,1,2,1).setWeight(50,50).setInsets(1));

        JLabel listenerLabel = new JLabel("加 载 许 可:");
        JTextField listenerField = new JTextField(40);
        JButton listenerButton = new JButton("选择");
        listenerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();

                fileChooser.setFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File f){
                        if(f.getName().endsWith("lic") || f.isDirectory()) return true;
                        return false;
                    }

                    @Override
                    public String getDescription(){
                        // TODO Auto-generated method stub
                        return "蜘蛛许可文件(*.lic)";
                    }
                });

                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.setDialogTitle("选择许可文件");
                int result = fileChooser.showOpenDialog(new JPanel());
                if(result == JFileChooser.APPROVE_OPTION){
                    File file = fileChooser.getSelectedFile();
                    String filePath  = file.getAbsolutePath();
                    FileTool.copyFile(filePath,System.getProperty("user.home"));
                    listenerField.setText(filePath);
                    Permitted.verifyLicense(filePath);
                }
            }
        });
        jd.add(listenerLabel,new GBC(0,2,1,1).setWeight(50,50).setInsets(1));
        jd.add(listenerField,new GBC(1,2,1,1).setWeight(50,50).setInsets(1));
        jd.add(listenerButton,new GBC(2,2,1,1).setWeight(50,50).setInsets(1));

        jd.setVisible(true);
    }
}
